package com.zjff.myshop.front.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zjff.myshop.entity.Category;
import com.zjff.myshop.exception.BusinessException;
import com.zjff.myshop.front.pojo.vo.CategoryVO;
import com.zjff.myshop.front.service.CategoryService;
import com.zjff.myshop.mapper.CategoryMapper;
import com.zjff.myshop.util.BeanUtils;
import com.zjff.myshop.util.PageList;
import com.zjff.myshop.util.PageQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    /**
     * 增加
     */
    @Override
    public Boolean insert(Category category) {
        Category temp = categoryMapper.selectByName(category.getName());
        if (temp != null) {
            throw new BusinessException("已存在名称相同的商品分类");
        }
        return categoryMapper.insert(category) > 0;
    }

    /**
     * 删除（单个）
     */
    @Override
    public Boolean deleteById(Integer id) {
        //检查记录是否存在
        Category category = categoryMapper.selectByPrimaryKey(id);
        if (category == null) {
            throw new BusinessException("id对应的分类不存在");
        }

        //删除分类数据
        return categoryMapper.deleteByPrimaryKey(id) > 0;
    }

    /**
     * 删除（多个）
     */
    @Override
    public Boolean deleteBatch(Integer[] ids) {
        return categoryMapper.deleteBatch(ids) > 0;
    }

    /**
     * 更改
     */
    @Override
    public Boolean update(Category category) {
        //检查参数
        Integer categoryId = category.getId();
        if (categoryId == null || categoryId <= 0) {
            throw new BusinessException("参数错误");
        }
        //更新
        return categoryMapper.update(category) > 0;
    }

    /**
     * 查询详情（根据id)
     */
    @Override
    public Category selectByPrimaryKey(Integer id) {
        return categoryMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询列表（全部)
     */
    @Override
    public List<CategoryVO> selectVOList() {
        //返回所有记录
        List<Category> categoryList = categoryMapper.selectList();
        // 列表复制返回
        return BeanUtils.copyList(categoryList, CategoryVO.class);
    }

    /**
     * 查询分页
     */
    @Override
    public PageList<CategoryVO> selectPage(PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getLimit());
        List<Category> categoryList = categoryMapper.selectList();
        int total = (int) new PageInfo<>(categoryList).getTotal();

        List<CategoryVO> categoryVOList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(categoryList)) {
            categoryVOList = BeanUtils.copyList(categoryList, CategoryVO.class);
        }
        return new PageList<>(categoryVOList, total, pageQuery.getLimit(), pageQuery.getPage());
    }
}
